package com.zt.dev.standardManage.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.sun.org.apache.bcel.internal.generic.SWITCH;
import com.zt.dev.standardManage.entity.enmu.CategoryEnum;
import com.zt.dev.standardManage.entity.vo.Category;
import com.zt.dev.standardManage.entity.vo.Model;
import com.zt.dev.standardManage.entity.vo.ResultVO;
import com.zt.dev.standardManage.entity.vo.Standards;
import com.zt.dev.standardManage.mapper.ModelsMapper;
import com.zt.dev.standardManage.mapper.StandardMapper;
import com.zt.dev.standardManage.service.StandardService;
import com.zt.dev.standardManage.utils.DateUtil;
import com.zt.dev.standardManage.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StandardServiceImpl implements StandardService {

    @Autowired
    private StandardMapper standardMapper;
    @Autowired
    private ModelsMapper modelsMapper;

    /**
     * 添加评估准则
     * @param standards
     * @return
     */
    @Override
    public int insertStandard(Standards standards) {
        String createTime = DateUtil.getStringDate();
        standards.setCreateTime(createTime);
        standards.setFrequency(0);
        //判断准则类别
        Integer node = CategoryEnum.getCode(standards.getCategory());
        standards.setCategoryNode(node);
        int result = standardMapper.insertStandard(standards);
        //新增准则成功
        if(result > 0){
            int size = 0;
            //生成准则
            if("true".equals(standards.getFlag())){
                Model model = new Model();
                model.setModelName(standards.getStandardName());
                model.setTarget(standards.getTarget());
                model.setContext(standards.getContext());
                model.setMethod(standards.getMethod());
                model.setCategoryNode(standards.getCategoryNode());
                model.setFrequency(0);
                model.setCreater(standards.getCreater());
                model.setCreateTime(createTime);
                size = modelsMapper.insertModels(model);
                if(size > 0){
                    return result;
                }else{
                    result--;
                }
            }
        }
        return result;
    }

    /**
     * 分页获取准则列表
     * @return
     */
    @Override
    public PageBean<Standards> getAllStandard(int currentPage, int pageSize,String standardName) {
        //获得总数
        int count = standardMapper.getCount(standardName);
        PageBean pageBean = PageBean.pageCommon(count, currentPage, pageSize);
        //起始 结束 页
        int start = (currentPage-1)*pageBean.getPageSize();
        int end = pageBean.getPageSize();
        //封装每页显示的数据
        List<Standards> standardsList = standardMapper.getAllStandard(start,end,standardName);
        for(Standards standards:standardsList){
            standards.setCategory(CategoryEnum.getType(standards.getCategoryNode()));
        }
        pageBean.setLists(standardsList);
        return pageBean;
    }

    /**
     * 删除评估准则
     * @param ids
     * @return
     */
    @Override
    public int deleteStandard(Integer[] ids) {
        int result = 0;
        for(Integer id : ids){
            result = standardMapper.deleteStandard(id);
            result++;
        }
        return result;
    }

    /**
     * 修改评估准则
     * @param standards
     * @return
     */
    @Override
    public int updateStandardById(Standards standards) {
        String category = standards.getCategory();
        if(category != null){
            Integer node = CategoryEnum.getCode(category);
            standards.setCategoryNode(node);
        }
        return standardMapper.updateStandardById(standards);
    }

    /**
     * 根据名称查询
     * @param standardName
     * @return
     */
    @Override
    public List<Standards> getStandardByName(String standardName) {
        return standardMapper.getStandardByName(standardName);
    }

    @Override
    public Standards getStandardById(Integer id) {
        Standards standards = standardMapper.getStandardById(id);
        standards.setCategory(CategoryEnum.getType(standards.getCategoryNode()));
        return standards;
    }


    /**
     * 获取准则列表
     * @return
     */
    @Override
    public List<Standards> getStandardList() {
        return standardMapper.getStandardList();
    }

    /**
     * 获取全部准则类别
     * @return
     */
    @Override
    public List<String> getAllType() {
        List<String> nameList = new ArrayList<>();
        List<Category> categoryList = standardMapper.getAllType();
        for(Category category : categoryList){
            nameList.add(category.getName());
        }
        return nameList;
    }

    /**
     * 统计当前月各类别量
     * @return
     */
    @Override
    public Map<String, Object> getStandardByMon(String createTime) {
        Map<String,Object> resultMap = new HashMap<>();
        List<ResultVO> resultVOList = new ArrayList<>();     //结果集
        String time = null;
        List<Category> categoryList = standardMapper.getAllType();     //获取类别
        for(int i = 0; i < categoryList.size();i++){
            ResultVO resultVO = standardMapper.getStandardByMon(i,createTime);
            if(resultVO != null){
                time = resultVO.getTime();
                resultVO.setCategory(categoryList.get(i).getName());
                resultVOList.add(resultVO);
            }else{
                ResultVO resultVO1 = new ResultVO();
                time = DateUtil.getStringDate();
                if(createTime != null) {
                    time = time.substring(0,7);  //截取月份
                }else{
                    int year = Integer.parseInt(time.substring(0,4));
                    int month = Integer.parseInt(time.substring(5,7));
                    String time1 = time.substring(5,7);
                    if(time1.indexOf('0')== 0){
                        time1 = time1.substring(1,2);
                        month = Integer.parseInt(time1);
                        if (month == 1) {
                            month = 12;
                        } else {
                            month = month - 1;
                        }
                    }else{
                        month = month - 1;
                    }

                    time = year + "-" + month;
                }
                resultVO1.setCategory(categoryList.get(i).getName());
                resultVO1.setNumber(0);
                resultVOList.add(resultVO1);
            }
        }
        String title = time+"各类别准则生成数量比";
        resultMap.put("title",title);
        resultMap.put("list",resultVOList);
        return resultMap;
    }

    /**
     * 统计当月准则、模板所占量
     * @return
     */
    @Override
    public Map<String,Object> getDataByType(String createTime){
        //返回值
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,Object> resultList = new HashMap<>();
        ResultVO standardVO = null;
        ResultVO modelVO = null;
        String time = null;
        for(int i=0;i<=2;i++){
            List<ResultVO> typeList = new ArrayList<>();
            //准则统计量
            standardVO = standardMapper.getStandardByMon(i,createTime);
            if(standardVO != null){
                standardVO.setName("准则");
                typeList.add(standardVO);
                time = standardVO.getTime();
            }else{
                standardVO = new ResultVO();
                standardVO.setName("准则");
                standardVO.setNumber(0);
                time = DateUtil.getStringDate();
                if(createTime != null) {
                    time = time.substring(0,7);  //截取月份
                }else{
                    int year = Integer.parseInt(time.substring(0,4));
                    int month = Integer.parseInt(time.substring(5,7));
                    String time1 = time.substring(5,7);
                    if(time1.indexOf('0')== 0){
                        time1 = time1.substring(1,2);
                        month = Integer.parseInt(time1);
                        if (month == 1) {
                            month = 12;
                        } else {
                            month = month - 1;
                        }
                    }else{
                        month = month - 1;
                    }

                    time = year + "-" + month;
                }
                typeList.add(standardVO);
            }
            modelVO = modelsMapper.getModelByMon(i,createTime);
            if(modelVO != null){
                modelVO.setName("模板");
                modelVO.setTime(time);
                typeList.add(modelVO);
            }else{
                modelVO = new ResultVO();
                modelVO.setName("模板");
                modelVO.setNumber(0);
                time = DateUtil.getStringDate();
                if(createTime != null) {
                    time = time.substring(0,7);  //截取月份
                }else{
                    int year = Integer.parseInt(time.substring(0,4));
                    int month = Integer.parseInt(time.substring(5,7));
                    String time1 = time.substring(5,7);
                    if(time1.indexOf('0')== 0){
                        time1 = time1.substring(1,2);
                        month = Integer.parseInt(time1);
                        if (month == 1) {
                            month = 12;
                        } else {
                            month = month - 1;
                        }
                    }else{
                        month = month - 1;
                    }

                    time = year + "-" + month;
                }
                typeList.add(modelVO);
            }
            String type = "category"+i;
            resultList.put(type,typeList);

        }
        String title = time + "准则、模板所占比";
        resultMap.put("title",title);
        resultMap.put("list",resultList);
        return resultMap;
    }

    /**
     *
     * 展示页面折线图,柱状图数据
     * @param createTime
     * @return
     */
    @Override
    public Map<String,Object> getDataLast(String createTime){
        Map<String,Object> resultMap = new HashMap<>();    //最终结果集
        String title = "准则生成量";                        //标题
        String[] xData = null;                             //x轴数据
        List<Map<String,Object>> resultList = new ArrayList<>();    //数据集
        String time = null;        //时间
        //获取类别
        List<Category> categoryList = standardMapper.getAllType();
        List<Integer[]> typeList = new ArrayList<>();   //存放类别集
        //对x轴数据处理
        List<ResultVO> standardList = standardMapper.getStandardLast(createTime);   //查询结果
        if(standardList.size() > 0) {
            time = standardList.get(0).getTime();
            Map<String,Object> timeMap = getTimeData(time,false);
            time = (String) timeMap.get("time");
            int days =(int) timeMap.get("days");
            xData = new String[days];
            for(int i = 0;i < categoryList.size();i++){
                Integer[] type = new Integer[days];
                //初始化y轴数据
                for(int j = 0;j<days;j++){
                    type[j] = 0;
                }
                typeList.add(type);
            }
            //处理x轴数据
            for (int i = 0; i < days; i++) {
                String day = (i + 1) + "日";
                xData[i] = time + day;
            }
            //填充y轴数据
            for(int j = 0;j<standardList.size();j++){
                String time1 = standardList.get(j).getTime();
                time1 = time1.substring(8,10);
                if(time1.indexOf('0')==0){
                    time1 = time1.substring(1,2);
                }
                Integer day = Integer.valueOf(time1);
                //判断为何种类别
                int flag = standardList.get(j).getCategoryNode();
                typeList.get(flag)[day-1] = standardList.get(j).getNumber();
            }
            for(int i = 0;i < categoryList.size();i++){
                Map<String,Object> map = new HashMap<>();
                map.put("name",categoryList.get(i).getName());
                map.put("yData",typeList.get(i));
                resultList.add(map);
            }
            resultMap.put("title",time+title);
            resultMap.put("xData",xData);
            resultMap.put("lists",resultList);
        }else{
            //各类别都未生成
            String currentTime = DateUtil.getStringDate();
            Map<String,Object> timeMap = getTimeData(currentTime,true);
            time = (String) timeMap.get("time");
            int days =(int) timeMap.get("days");
            for(int i = 0;i < categoryList.size();i++){
                Integer[] type = new Integer[days];
                //初始化y轴数据
                for(int j = 0;j<days;j++){
                    type[j] = 0;
                }
                typeList.add(type);
            }
            xData = new String[days];
            for(int i = 0; i < days; i++){
                String day = (i+1)+"日";
                xData[i] = time + day;
            }
            for(int i = 0;i < typeList.size();i++){
                Map<String,Object> map = new HashMap<>();
                map.put("name",categoryList.get(i).getName());
                map.put("yData",typeList.get(i));
                resultList.add(map);
            }
            resultMap.put("title",time+"各类别生成数量");
            resultMap.put("xData",xData);
            resultMap.put("lists",resultList);
        }
        return resultMap;
    }

    /**
     * 首页柱状图，准则模板总量
     * @param createTime
     * @return
     */
    @Override
    public Map<String,Object> getData(String createTime){
        Map<String,Object> resultMap = new HashMap<>();                  //返回结果集
        List<Map<String,Object>> resultVOList = new ArrayList<>();       //返回lists
        Map<String,Object> result = null;                                //list中结果集
        String[] xData = null;                                           //x轴数据
        String time = null;                                              //日期
        Integer[] snumber = null;                                        //准则y轴数据
        Integer[] mnumber = null;                                        //模板y轴数据
        List<ResultVO> standardList = standardMapper.getDataLast(createTime);
        List<ResultVO> modelList = modelsMapper.getModelDataLast(createTime);
        //对结果集长度进行处理
        if(standardList.size() != 0 || modelList.size() != 0){
            if(standardList.size() == 0){
                time = modelList.get(0).getTime();
            }else if(modelList.size() != 0){
                time = standardList.get(0).getTime();
            }else{
                time = standardList.get(0).getTime();
            }
            Map<String,Object> timeMap = getTimeData(time,false);
            time = (String) timeMap.get("time");
            int days =(int) timeMap.get("days");
            //设置x轴的长度
            xData = new String[days];
            snumber = new Integer[days];
            mnumber = new Integer[days];
            for(int i=0;i<days;i++){
                String day = (i+1)+"日";
                xData[i] = time + day;
                //设置y轴初始值为0
                snumber[i] = 0;
                mnumber[i] = 0;
            }
        }

        //填充数据
        if(standardList.size() > 0){
            result = new HashMap<>();
            //上一个月准则数量，根据node和日期求和,对yData进行处理
           for(int m=0;m<standardList.size();m++){
               String time1 = standardList.get(m).getTime();
               time1 = time1.substring(8,10);
               if(time1.indexOf('0')==0){
                   time1 = time1.substring(1,2);
               }
               Integer day = Integer.valueOf(time1);
               snumber[day-1] = standardList.get(m).getNumber();
           }
           result.put("name","准则");
           result.put("yData",snumber);
           resultVOList.add(result);
        }else{
            result = new HashMap<>();
            result.put("name","准则");
            result.put("yData",snumber);
            resultVOList.add(result);
        }
        if(modelList.size() > 0){
            result = new HashMap<>();
            for(int n = 0;n<modelList.size();n++){
                String time1 = modelList.get(n).getTime();
                time1 = time1.substring(8,10);
                if(time1.indexOf('0')==0){
                    time1 = time1.substring(1,2);
                }
                Integer day = Integer.valueOf(time1);
                mnumber[day-1] = modelList.get(n).getNumber();
            }
            result.put("name","模板");
            result.put("yData",mnumber);
            resultVOList.add(result);
        }else{
            result = new HashMap<>();
            result.put("name","模板");
            result.put("yData",mnumber);
            resultVOList.add(result);
        }
        resultMap.put("title",time+"准则、模板当日总量折线图");
        resultMap.put("xData",xData);
        resultMap.put("lists",resultVOList);
        return resultMap;
    }

    /**
     * 处理首页折线图数据
     * @param createTime
     * @return
     */
    @Override
    public Map<String,Object> getDataLine(String createTime){
        //最终返回结果集
        Map<String,Object> resultMap = new HashMap<>();
        String[] xData = null;   //x轴数据
        List<Map<String,Object>> resultList = new ArrayList<>();   //数据集
        //获取类别
        List<Category> categoryList = standardMapper.getAllType();
        List<Integer[]> typeList = new ArrayList<>();   //存放类别集
        List<ResultVO> resultVOList = standardMapper.getStandardData(createTime);
        String time = null;
        if(resultVOList.size() > 0){
            //对时间进行处理
            time = resultVOList.get(0).getTime();
            Map<String,Object> timeMap = getTimeData(time,false);
            time = (String) timeMap.get("time");
            int days =(int) timeMap.get("days");
            //设置x轴的长度
            xData = new String[days];
            for(int i = 0;i < categoryList.size();i++){
                Integer[] type = new Integer[days];
                //初始化y轴数据
                for(int j = 0;j<days;j++){
                    type[j] = 0;
                }
                typeList.add(type);
            }
            for(int i=0;i<days;i++){
                String day = (i+1)+"日";
                xData[i] = time + day;
            }
            int flag;
            for(int j = 0;j < resultVOList.size();j++){
                //获取数据的日期
                String time1 = resultVOList.get(j).getTime();
                time1 = time1.substring(8,10);
                if(time1.indexOf('0')==0){
                    time1 = time1.substring(1,2);
                }
                Integer day = Integer.valueOf(time1);
                //判断为何种类别
                flag = resultVOList.get(j).getCategoryNode();
                typeList.get(flag)[day-1] = resultVOList.get(j).getNumber();
            }
            for(int i = 0;i < categoryList.size();i++){
                Map<String,Object> map = new HashMap<>();
                map.put("name",categoryList.get(i).getName());
                map.put("yData",typeList.get(i));
                resultList.add(map);
            }
            resultMap.put("title",time+"各类别生成数量");
            resultMap.put("xData",xData);
            resultMap.put("lists",resultList);
        }else{
            //各类别都未生成
            String currentTime = DateUtil.getStringDate();
            boolean flag = true;
            Map<String,Object> timeMap = getTimeData(currentTime,flag);
            time = (String) timeMap.get("time");
            int days =(int) timeMap.get("days");
            for(int i = 0;i < categoryList.size();i++){
                Integer[] type = new Integer[days];
                //初始化y轴数据
                for(int j = 0;j<days;j++){
                    type[j] = 0;
                }
                typeList.add(type);
            }
            xData = new String[days];
            for(int i = 0; i < days; i++){
                String day = (i+1)+"日";
                xData[i] = time + day;
            }
            for(int i = 0;i < typeList.size();i++){
                Map<String,Object> map = new HashMap<>();
                map.put("name",categoryList.get(i).getName());
                map.put("yData",typeList.get(i));
                resultList.add(map);
            }
            resultMap.put("title",time+"各类别生成数量");
            resultMap.put("xData",xData);
            resultMap.put("lists",resultList);
        }
        return resultMap;
    }


    //处理x轴时间数据
    public Map<String,Object> getTimeData(String time,boolean flag){
        Map<String,Object> result = new HashMap<>();
        //对时间进行处理
        int year = Integer.parseInt(time.substring(0,4));   //年份
        time = time.substring(5,7);  //截取月份
        //对月份进行处理
        int month = 0;
        if(time.indexOf('0') == 0){
            month = Integer.parseInt(time.substring(1));
            if(flag){
                if(month == 1){
                    month = 12;
                }else{
                    month = month -1;
                }
                time = month + "月";
            }else{
                time = month + "月";
            }
        }else{
            if(flag){
                month = month -1;
                time = month + "月";
            }else{
                time = month + "月";
            }
        }
        //获取当前年月的天数
        int days = DateUtil.getDaysByYearMonth(year,month);
        result.put("time",time);
        result.put("days",days);
        return result;
    }

}
